const express= require('express');
const postMudle = require('../modlels/yh');
const crypto = require('crypto');
const router = express.Router();

router.get('/login',function(req,res){
	res.render('login',{
		title:'登录',
		user:req.session.user,
		yh:req.session.yh,
	})
}) 

router.post('/login',function(req,res){
	var uname = req.body.uname;
	postMudle.findOne({uname:uname}).exec(function(err,data){
		if(err){
			return res.json({error:1,message:'系统错误'});
		}
		
		if(!data){
			return res.json({error:1,message:'用户名不存在，请尼玛去注册'});
		}
		var md5 = crypto.createHash('md5');
		var password =  md5.update(req.body.password).digest('hex');
		console.log(password);
		console.log(data.password)
		if(password != data.password){
			return res.json({error:1,message:'密码错误'});
		}
		
		 req.session.yh = data;
		 res.json({error:0,message:'用户登录成功'});	
	})
})




module.exports = router;




